package com.qf.smsplatform.strategy.mq;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.index.StandardSubmit;
import com.qf.smsplatform.strategy.service.StrategyManager;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.support.AmqpHeaders;

import org.springframework.cloud.stream.annotation.StreamListener;

import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;


@Component
@Slf4j
public class PreSendListener {

    @Resource
    private StrategyManager strategyManager;

    @StreamListener(RabbitMQConstants.TOPIC_PRE_SEND)
    public  void  listen(StandardSubmit submit,
                         @Header (name = AmqpHeaders.CHANNEL) Channel channel,
                         @Header(name = AmqpHeaders.DELIVERY_TAG)Long deliveryTag) throws IOException {
        //1.使用@Header接收channel和DeliveryTag，首先要在配置文件中手动ack
        //2.在配置文件中设置消费者组，避免重复消费
        //3.输出获取到的消息

        log.info("【策略模块】 获取到消息 submit = {}",submit);

        //4.执行全部策略的过滤器链
        strategyManager.execute(submit);

        //消息消费成功，手动ack
        channel.basicAck(deliveryTag,false);

    }

}
